package com.byit.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.byit.builder.MapBuilder;
import com.byit.domain.SysDict;
import com.byit.domain.vo.DictVo;
import com.byit.mapper.SysDictMapper;
import com.byit.service.ISysDictService;
import com.byit.utils.PageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统码表 服务实现类
 * </p>
 *
 * @author wangx
 * @since 2018-12-11
 */
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements ISysDictService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private SysDictMapper dictMapper;

    @Override
    public List<DictVo> selectDictByType(String dictType) {
        List<JSONObject> list = dictMapper.selectDictByType(dictType);
        return transformTypeName(list);
    }

    @Override
    public List<JSONObject> selectAppNewsByType(String dictType) {
        Map<String, Object> params = MapBuilder.newMap(4).setParam("dictType", dictType)
                .ok();
        List<JSONObject> list = dictMapper.selectAppNewsByType(params);
        return list;
    }

    @Override
    public Page<JSONObject> selectDataPage(Page<SysDict> pageInfo, Map<String, Object> map) {
        List<JSONObject> list = this.baseMapper.selectDataPage(pageInfo,map);
        Page<JSONObject> page = PageUtil.transform(pageInfo);
        page.setRecords(list);
        return page;
    }

    /**
     * 码表转换
     *
     * @param list 码表数据
     * @return
     */
    private List<DictVo> transformTypeName(List<JSONObject> list) {
        List<DictVo> listVo = new ArrayList<>(list.size());
        Iterator<JSONObject> it = list.iterator();
        while (it.hasNext()) {
            JSONObject obj = it.next();
            listVo.add(new DictVo(String.valueOf(obj.get("dictCode")), String.valueOf(obj.get("dictValue"))));
        }
        return listVo;
    }
}
